import ephem
import arrow


def get_sidereal_time(lat, lon, elev):
    city = ephem.Observer()
    city.lat = lat
    city.lon = lon
    city.elev = float(elev)
    city.date = str(arrow.utcnow().format('YYYY/MM/DD HH:mm:ss'))
    return str(city.sidereal_time())


def calc_hour_angle(sidereal):
    [hour, minute, second] = sidereal.split(':')
    total_seconds = float(hour)*3600 + float(minute)*60 + float(second)
    shift_seconds = 2*3600 + 55*60 + 37.7

    if total_seconds >= shift_seconds:
        result_seconds = total_seconds - shift_seconds
    else:
        result_seconds = 24*3600 + total_seconds - shift_seconds

    polaris_second = int(result_seconds % 60)
    polaris_minute = int(((result_seconds - polaris_second) % 3600) / 60)
    polaris_hour = int(result_seconds / 3600)

    return "{0}:{1}:{2}".format(polaris_hour, polaris_minute, polaris_second)


if __name__ == '__main__':
    sidereal_time = get_sidereal_time('22:33:50', '114:03:39', '50')
    print(calc_hour_angle(sidereal_time))
